package org.vectrics.recruiter.web.tags;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;
import org.vectrics.user.User;

public class JobCandidateInterviewsTag extends VectricsTagSupport {

    private static final long serialVersionUID = 2985514196560338479L;

    private static Category log = Category.getInstance(JobCandidateInterviewsTag.class);

    private String jobId = null;

    private Job job = null;

    private String userId = null;

    private User user = null;

    private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");

    public JobCandidateInterviewsTag() {
    }

    @Override
    public String makeStartTag() {
        log.debug("doRender() called");

        Table mainTable = new Table();
        mainTable.setWidth("90%");
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();

        jobId = getRequest().getParameter("jobId");
        userId = getRequest().getParameter("userId");
        Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(userId));
        CandidateInfo candidate = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(person);

        if (jobId != null) {
            try {
                job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
            } catch (Exception ex) {
                log.error("Exception finding job = " + jobId, ex);
            }

            if (job == null) {
                log.error("doRender() - could not find job for job id = " + jobId);
            } else {
                if (user == null) {
                    log.error("doRender() - could not find userProfile.  User Id = " + userId);
                } else {
                    mainCell.addElement(TextHelper.getFont("header2", "<br><br>Interviews"));

                    Table summaryTable = new Table();
                    mainCell.addElement(summaryTable);

                    JobCandidate jobCandidate = job.findJobCandidate(candidate);

                    Collection interviews = RecruitServiceLocator.getJobProvider().findJobCandidateInterviews(
                            jobCandidate);
                    Iterator iter = interviews.iterator();

                    mainCell.addElement(TextHelper.getFont("header2", "<br>"));
                    BorderTable table = new BorderTable();
                    table.setWidth(800);
                    mainCell.addElement(table);
                    Row hdrRow = table.addRow();
                    Cell hdrCell = hdrRow.addCell();
                    hdrCell.addElement(TextHelper.getFont("subFont", "<b>Date</b>"));

                    hdrCell = hdrRow.addCell();
                    hdrCell.setWidth("30%");
                    hdrCell.addElement(TextHelper.getFont("subFont", "<b>Person</b>"));

                    hdrCell = hdrRow.addCell();
                    hdrCell.setWidth("30%");
                    hdrCell.addElement(TextHelper.getFont("subFont", "<b>Result</b>"));

                    hdrCell = hdrRow.addCell();
                    hdrCell.setWidth("20%");
                    hdrCell.addElement(TextHelper.getFont("subFont", "<b>Action</b>"));

                    if (iter.hasNext()) {
                        while (iter.hasNext()) {
                            Row row = table.addRow();
                            Interview interview = (Interview) iter.next();
                            Cell cell = row.addCell();
                            cell.addElement(TextHelper.getFont("subFont",
                                    dateFormatter.format(interview.getDateStart())));

                            cell = row.addCell();
                            if (interview.getLeadPerson() != null) {
                                cell.addElement(TextHelper.getFont("subFont", interview.getLeadPerson().getName()));
                            }

                            cell = row.addCell();
                            cell.addElement(TextHelper.getFont("subFont", interview.getStatusType().getName()));

                            cell = row.addCell();
                            A link = new A();
                            link.setHref("/hrs-public/admin/job/candidate/interview");
                            link.addElement("change");
                            link.setClass("subLink");
                            cell.addElement(link);
                        }
                    } else {
                        Row row = table.addRow();
                        Cell cell = row.addCell();
                        cell.setColSpan(4);
                        cell.addElement(TextHelper.getFont("subFont",
                                "<i>" + this.getPhrase("phrase.interviews.none.for.candidate") + "</i>"));
                    }

                    mainCell.addElement("<br><br>");

                }
            }
        } else {
            log.error("doRender() - job parameter not passed to this page.");
        }

        return mainTable.toString();
    }

    protected void showField(final Table table, final String title, final String value) {
        Row row = table.addRow();
        Cell cellA = row.addCell();
        cellA.addElement(TextHelper.getFont("subFont", title));
        Cell cellSep = row.addCell();
        cellSep.addElement("&nbsp;&nbsp;");
        Cell cellB = row.addCell();
        // if ((value != null) && (value.trim().length() > 0)) {
        Table valueTable = new BorderTable();
        valueTable.setWidth(300);
        cellB.addElement(valueTable);
        Cell valueCell = valueTable.addRow().addCell();
        if (value != null) {
            valueCell.addElement(TextHelper.getFont("subFont", value));
        }
        if (value == null || value.trim().length() <= 0) {
            log.debug("force space to hold box height to non-zero because no text was put in box for value.");
            valueCell.addElement(TextHelper.getFont("subFont", "&nbsp;"));
        }
        valueCell.setClass("xltGray");
        // cellB.addElement("<br>&nbsp;");

        // }

    }

}